﻿Imports Microsoft.Phone.Shell

Namespace Controls
    Public Class BindableApplicationBarMenuItem
        Inherits FrameworkElement
        Implements IApplicationBarMenuItem

        Public Sub New()
            MenuItem = New ApplicationBarMenuItem
            MenuItem.Text = "Text"
            AddHandler MenuItem.Click, AddressOf ApplicationBarIconButtonClick
        End Sub

        Public Shared ReadOnly CommandProperty As DependencyProperty = DependencyProperty.RegisterAttached("Command", GetType(ICommand), GetType(BindableApplicationBarMenuItem), Nothing)

        Public Property Command As ICommand
            Get
                Return CType(GetValue(CommandProperty), ICommand)
            End Get
            Set(value As ICommand)
                SetValue(CommandProperty, value)
            End Set
        End Property

        Public Shared ReadOnly CommandParameterProperty As DependencyProperty = DependencyProperty.RegisterAttached("CommandParameter", GetType(Object), GetType(BindableApplicationBarMenuItem), Nothing)

        Public Property CommandParameter As Object
            Get
                Return GetValue(CommandParameterProperty)
            End Get
            Set(value As Object)
                SetValue(CommandParameterProperty, value)
            End Set
        End Property

        Public Shared ReadOnly CommandParameterValueProperty As DependencyProperty = DependencyProperty.RegisterAttached("CommandParameterValue", GetType(Object), GetType(BindableApplicationBarMenuItem), Nothing)

        Public Property CommandParameterValue As Object
            Get
                Return GetValue(CommandParameterValueProperty)
            End Get
            Set(value As Object)
                SetValue(CommandParameterValueProperty, value)
            End Set
        End Property

        Public Shared ReadOnly IsEnabledProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsEnabled", GetType(Boolean), GetType(BindableApplicationBarMenuItem), New PropertyMetadata(True, AddressOf OnEnabledChanged))

        Private Shared Sub OnEnabledChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            If e.NewValue <> e.OldValue Then
                CType(d, BindableApplicationBarMenuItem).MenuItem.IsEnabled = CBool(e.NewValue)
            End If
        End Sub

        Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.RegisterAttached("Text", GetType(String), GetType(BindableApplicationBarMenuItem), New PropertyMetadata(AddressOf OnTextChanged))

        Private Shared Sub OnTextChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
            If e.NewValue <> e.OldValue Then
                CType(d, BindableApplicationBarMenuItem).MenuItem.Text = e.NewValue.ToString
            End If
        End Sub

        Public Event Click(sender As Object, e As EventArgs) Implements IApplicationBarMenuItem.Click

        Public Property IsEnabled As Boolean Implements IApplicationBarMenuItem.IsEnabled
            Get
                Return CBool(GetValue(IsEnabledProperty))
            End Get
            Set(value As Boolean)
                SetValue(IsEnabledProperty, value)
            End Set
        End Property

        Public Property Text As String Implements IApplicationBarMenuItem.Text
            Get
                Return CStr(GetValue(TextProperty))
            End Get
            Set(value As String)
                SetValue(TextProperty, value)
            End Set
        End Property

        Public Property MenuItem As ApplicationBarMenuItem

        Private Sub ApplicationBarIconButtonClick(sender As Object, e As EventArgs)
            If Command IsNot Nothing AndAlso CommandParameter IsNot Nothing Then
                Command.Execute(CommandParameter)
            ElseIf Command IsNot Nothing Then
                Command.Execute(CommandParameterValue)
            End If
        End Sub
    End Class
End Namespace